﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyUtil;

namespace MebAppZip
{
    class Program
    {
        static void Main(string[] args)
        {
            var targetPath = args[0];
            var dir = Path.GetDirectoryName(targetPath);
            var targetFile = Path.GetFileName(targetPath);
            var targetFileConfig = targetFile + ".config";
            var appJsonFile = Path.Combine(dir, "app.json");
            if (!File.Exists(appJsonFile))
            {
                Console.WriteLine("file app.json is required");
            }
            else
            {
                var appJson = JsonFile.Read<AppInfo>(appJsonFile);
                appJson.Files.Clear();
                appJson.Files.Add(targetFile);
                appJson.Files.Add(targetFileConfig);
                var dlls = Directory.EnumerateFiles(dir, "*.dll").OrderBy(x => x);
                foreach (var item in dlls)
                {
                    appJson.Files.Add(Path.GetFileName(item));
                }
                JsonFile.Write(appJsonFile, appJson);


                var zipFiles = new List<string>();
                foreach (var item in appJson.Files)
                {
                    var fileName = Path.Combine(dir, item);
                    zipFiles.Add(fileName);
                }
                zipFiles.Add(appJsonFile);
                ZipHelper.ZipFiles(dir, zipFiles.ToArray(), Path.Combine(dir, appJson.AppID + ".zip"));
                Console.WriteLine("zip ok.");
            }
        }
    }
}
